var common = {};
common.isWord = function () {
    var cs = captureScreen();
    if (images.detectsColor(cs, "#ECFFFF", 67, 640, 16, "diff")) {
        cs.recycle();
        return true;
    } else {
        cs.recycle();
        return false;
    }
};

common.random = function (max, min) {
    return Math.random() * (max - min) + min;
};
common.unionMember = function () {
    var imgPath = "/sdcard/rok_stat/upload/" + new Date().getTime() + ".png";
    var cs = images.captureScreen(imgPath);
    var src = images.read(imgPath);
    var clip = images.clip(src, 1014, 234, 70, 255);
    images.save(clip, imgPath);
    var res = http.postMultipart(confModule.getUrl() + "ocr/unionMember", {
        file: open(imgPath)
    }, {
        headers: {
            'token': confModule.getToken()
        }
    });
    if (res.statusCode == 200) {
        var json = JSON.parse(res.body.string());
        if (json.code == "00000") {
            return json.data;
        }
    }
    src.recycle();
    clip.recycle();
    files.remove(imgPath)
    return [0, 0, 0, 0];
};
common.gameId = function () {
    var imgPath = "/sdcard/rok_stat/upload/" + new Date().getTime() + ".png";
    var cs = images.captureScreen(imgPath);
    var src = images.read(imgPath);
    var clip = images.clip(src, 570, 189, 200, 25);
    images.save(clip, imgPath);
    var result = 0;
    var res = http.postMultipart(confModule.getUrl() + "ocr/gameId", {
        file: open(imgPath)
    }, {
        headers: {
            'token': confModule.getToken()
        }
    });
    if (res.statusCode == 200) {
        var json = JSON.parse(res.body.string());
        if (json.code == "00000") {
            result = json.data;
        }
    }
    src.recycle();
    clip.recycle();
    files.remove(imgPath)
    return result;
};
common.currentPower = function () {
    var imgPath = "/sdcard/rok_stat/upload/" + new Date().getTime() + ".png";
    var cs = images.captureScreen(imgPath);
    var src = images.read(imgPath);
    var clip = images.clip(src, 526, 106, 260, 35);
    images.save(clip, imgPath);
    var result = 0;
    var res = http.postMultipart(confModule.getUrl() + "ocr/currentPower", {
        file: open(imgPath)
    }, {
        headers: {
            'token': confModule.getToken()
        }
    });
    if (res.statusCode == 200) {
        var json = JSON.parse(res.body.string());
        if (json.code == "00000") {
            result = json.data;
        }
    }
    src.recycle();
    clip.recycle();
    files.remove(imgPath)
    return result;
};
common.death = function () {
    var imgPath = "/sdcard/rok_stat/upload/" + new Date().getTime() + ".png";
    var cs = images.captureScreen(imgPath);
    var src = images.read(imgPath);
    var clip = images.clip(src, 700, 200, 370, 242);
    images.save(clip, imgPath);
    var result = [0, 0];
    var res = http.postMultipart(confModule.getUrl() + "ocr/death", {
        file: open(imgPath)
    }, {
        headers: {
            'token': confModule.getToken()
        }
    });
    if (res.statusCode == 200) {
        var json = JSON.parse(res.body.string());
        if (json.code == "00000") {
            result = json.data;
        }
    }
    src.recycle();
    clip.recycle();
    files.remove(imgPath)
    return result;
};
common.killCount = function () {
    var imgPath = "/sdcard/rok_stat/upload/" + new Date().getTime() + ".png";
    var cs = images.captureScreen(imgPath);
    var src = images.read(imgPath);
    var clip = images.clip(src, 660, 285, 200, 174);
    images.save(clip, imgPath);
    var result = [0, 0, 0, 0, 0];
    var res = http.postMultipart(confModule.getUrl() + "ocr/killCount", {
        file: open(imgPath)
    }, {
        headers: {
            'token': confModule.getToken()
        }
    });
    if (res.statusCode == 200) {
        var json = JSON.parse(res.body.string());
        if (json.code == "00000") {
            result = json.data;
        }
    }
    src.recycle();
    clip.recycle();
    files.remove(imgPath)
    return result;
};
common.enterNumber = function (num) {
    for (var i = 0; i < num.length; i++) {
        var str = num.substring(i, i + 1);
        if (str.toUpperCase() == "S") {
            continue;
        }
        if (str == "0") {
            str = "10";
        }
        commandModule.press((Number(str) - 1) * 128 + 66, 430, 10);
        sleep(this.random(500, 1000));
    }
};
common.enterNumber2 = function (num) {
    for (var i = 0; i < num.length; i++) {
        var str = num.substring(i, i + 1);
        switch (str) {
            case "0":
                commandModule.press(538, 683, 10);
                break;
            case "1":
                commandModule.press(266, 430, 10);
                break;
            case "2":
                commandModule.press(538, 430, 10);
                break;
            case "3":
                commandModule.press(810, 430, 10);
                break;
            case "4":
                commandModule.press(266, 517, 10);
                break;
            case "5":
                commandModule.press(538, 517, 10);
                break;
            case "6":
                commandModule.press(810, 517, 10);
                break;
            case "7":
                commandModule.press(266, 599, 10);
                break;
            case "8":
                commandModule.press(538, 599, 10);
                break;
            case "9":
                commandModule.press(810, 599, 10);
                break;
            default:
                continue;
        }
        sleep(this.random(500, 1000));
    }
};
common.getPoints = function (x, pr) {
    var img = captureScreen();
    var points = images.findAllPointsForColor(img, "#023C54", {
        region: [x, 220, 1, 460],
        threshold: 16
    })
    var pMin = 0;
    var pMax = 0;
    for (var i = 0; i < points.length; i++) {
        var p = points[i];
        if (pMin == 0) {
            pMin = p.y
        }
        if (pMax == 0) {
            pMax = p.y
        }
        if (p.y - pMax < 10) {
            pMax = p.y
        } else {
            if (pMax - pMin > 10) {
                pr.push({x: p.x, y: (pMax - pMin) / 2 + pMin})
            }
            pMin = p.y
            pMax = p.y
        }

        if (i == points.length - 1) {
            if (pMax - pMin > 10) {
                pr.push({x: p.x, y: (pMax - pMin) / 2 + pMin})
            }

        }
    }
};
common.getPoint = function (x) {
    var img = captureScreen();
    var point = images.findColor(img, "#00CBFF", {
        region: [x, 145, 1, 540],
        threshold: 16
    })
    return point;

};
common.getClipBtn = function () {
    var img = captureScreen();
    var point = images.findColor(img, "#157CAB", {
        region: [530, 220, 260, 25],
        threshold: 16
    })
    img.recycle();
    return point;
};
common.getCastle = function () {
    var img = captureScreen();
    var point = images.findColor(img, "#B5BAA9", {
        region: [480, 320, 300, 200],
        threshold: 16
    })
    return point;
};
common.waitColorVisible = function (x, y, color) {
    while (true) {
        var img = captureScreen();
        if (images.detectsColor(img, color, x, y, 16, "diff")) {
            break
        } else {
            sleep(100);
        }
    }
};
common.waitImgVisibleInRegion = function (tmplImgPath, x, y, w, h) {
    // var img = captureScreen();
    var tmpl = images.read(tmplImgPath);
    var points = findImage(captureScreen(), tmpl, {
        region: [x, y, w, h]
    });
    while (!points) {
        // img = captureScreen();
        //console.log("未找到匹配模板:" + tmplImgPath)
        points = findImage(captureScreen(), tmpl, {
            region: [x, y, w, h]
        });
        sleep(100);
    }
    // img.recycle();
    tmpl.recycle();
};
common.waitImgVisibleInRegionTimeOut = function (tmplImgPath, x, y, w, h) {
    // var img = captureScreen();
    var tmpl = images.read(tmplImgPath);
    var points = findImage(captureScreen(), tmpl, {
        region: [x, y, w, h]
    });
    var i = 0;
    while (!points && i < 50) {
        points = findImage(captureScreen(), tmpl, {
            region: [x, y, w, h]
        });
        sleep(100);
        i = i + 1
    }
    tmpl.recycle();
    return points
};
common.waitImgVisibleInRegionWithTimeOut = function (tmplImgPath, x, y, w, h, t) {
    // var img = captureScreen();
    var tmpl = images.read(tmplImgPath);
    var points = findImage(captureScreen(), tmpl, {
        region: [x, y, w, h]
    });
    var i = 0;
    while (!points && i < t) {
        points = findImage(captureScreen(), tmpl, {
            region: [x, y, w, h]
        });
        sleep(100);
        i = i + 1
    }
    tmpl.recycle();
    return points
};
common.waitImgVisibleInRegionClick = function (tmplImgPath, x, y, w, h, xx, yy) {
    // var img = captureScreen();
    var tmpl = images.read(tmplImgPath);
    var points = findImage(captureScreen(), tmpl, {
        region: [x, y, w, h]
    });
    var i = 0;
    while (!points && i < 50) {
        // img = captureScreen();
        //console.log("未找到匹配模板:" + tmplImgPath)
        points = findImage(captureScreen(), tmpl, {
            region: [x, y, w, h]
        });
        sleep(100);
        i = i + 1
    }
    if (i >= 50) {
        commandModule.press(xx, yy, 10)
        this.waitImgVisibleInRegion(tmplImgPath, x, y, w, h)
    }
    // img.recycle();
    tmpl.recycle();
};
common.imgExistInRegion = function (tmplImgPath, x, y, w, h) {
    var tmpl = images.read(tmplImgPath);
    var points = findImage(captureScreen(), tmpl, {
        region: [x, y, w, h]
    });
    tmpl.recycle();
    return points;
};

common.findImage = function (tmplImgPath) {
    var tmpl = images.read(tmplImgPath);
    var t = 0.9;
    var findResult = findImage(captureScreen(), tmpl, {
        threshold: t
    });
    var i = 0;
    while(!findResult && i < 50){
        t=t-0.01
        findResult = findImage(captureScreen(), tmpl, {
            threshold: t
        });
        sleep(100);
        i = i + 1
    }
    tmpl.recycle();
    return findResult;
}

module.exports = common;